import mongoose from 'mongoose';
import log4js from 'koa-log4';

const logger = log4js.getLogger('QueueWatcher');
class QueueWatcher {
  async handle({ user }) {
    if (!QueueWatcher.isMessageFromStudent(user)) return;

    const Queue = mongoose.model('Queue');

    const { nModified } = await Queue.updateOne({
      type: 1,
      jiuquId: user.getStudentJqId(),
    }, {
      $set: { resolved: true },
    });

    if (nModified > 0) {
      logger.debug('Receive Customer Message, %s mark queue record as resolved', user.jiuquId);
    }
  }

  static isMessageFromStudent(user) {
    return user.group.name === '学生';
  }
}

const queueWatcher = new QueueWatcher();

export { QueueWatcher };
export default queueWatcher;
